package com.str.HT;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.str.map.Map;

public class MapTest {
	
  private static Map createMap(int i) {
  	if (i == 1) return new CBHT();
    if (i == 2) return new OBHT();
  	return null;
  }
    
  public static void main(String[] args) throws IOException {
    BufferedReader stdin = new BufferedReader(
    	                   new InputStreamReader(System.in)); 	
  	   
    while (true) {
      System.out.println("1. Create CBHT");
      System.out.println("2. Create OBHT");	         
      System.out.print("\tEnter your choice(Enter to quit): ");          
      
      String inputLine = stdin.readLine();
      if (inputLine.equals("")) {
        System.out.println("End of run.");
        System.exit(0);
      }     
      
      int choice = Integer.parseInt(inputLine);
      Map strMap = createMap(choice);
      if (strMap == null) {   	
        System.out.println("Invalid choice.");
        continue;
      }
    
      Object key, value;    
      while (true) {
        System.out.println("-------------------------");
        System.out.println("     1. Clear");      
        System.out.println("     2. Add");    
        System.out.println("     3. Remove");
        System.out.println("     4. Serach");
        System.out.print("\tEnter your choice(Enter to quit): ");
      
        inputLine = stdin.readLine();      
        if (inputLine.equals("")) break;
        choice = Integer.parseInt(inputLine);

        switch (choice) {
          case 1:
            System.out.println("Cleared.");
            strMap.clear();
            break;

          case 2:
            System.out.print("Enter a key: ");       
            key = stdin.readLine();
            System.out.print("Enter a value: ");
            value = stdin.readLine();
            strMap.add(key, value);
            break;

          case 3:
            System.out.print("Enter a key: ");
            key = stdin.readLine();
            value = strMap.remove(key);
            if (value != null)
              System.out.println(value + " has been deleted.");
            else
              System.out.println("No found " + key);
            break;

          case 4:
            System.out.print("Enter a key: ");
            key = stdin.readLine();
            value = strMap.getValue(key);
            if (value != null)
              System.out.println("Found " + value);
            else
              System.out.println("No found " + key);
            break;

          default:
            System.out.println("Invalid choice.");
            continue;
        }
      
        System.out.println(strMap);
        System.out.println("Size: " + strMap.size());
      }
    }
  }
}
